<?php 
	include_once 'autoload.php';
	
	session_start();
	

	$folder = PictureConfig::$CONTENT_FOLDER;
	
	$id = $_GET['id'];
	
	// load the job from the database
	$service = new Service(); 
	$job = $service->getJob($id);
	
	$file = "{$folder}jobs/job_{$id}.dat";
	$s = HtmlUtil::getSessionUser(); 
	
	// in case the user is not logged, will just provide the small filler.
	if($s->isLogged()){
		if(is_file($file)){
			if($job != null){
				$name = $job->getName() . '.zip';
				$name = str_replace(" ", "_", $name);
				$name = str_replace("\t", "_", $name);
				
				$size = filesize($file);
				
				header("Content-Size: {$size}");
				header("Content-Disposition: attachment; filename={$name}");
				
				readfile($file);
				
				exit();
			}
			else {
				echo "the job associated with the file cannot be found";
			}
		}
		else {
			echo "I cannot find the file with id: {$id}";
		}
	}
	else {
		echo 'you are currently not logged in';
	}
?>